<?php
function users_info($context) {
	if ($context) {
		switch ($context) {
			case "modinfo":
				//
			break;
			case "help":
				//
			break;
			case "about":
				$page = array();
				$page['title'] = "About Users";
				$page['body'] = "<p>Safe-Harbour Users module designed by mike@ox-code.org</p>\n";
			break;
		}//switch context
	}
}
function setup_custom_icons($Q) {
	$T = array();
  $T{'absolute_path'} = $Q{'absolute_path'};
  $T{'icons'} = array("label" => "users", "location" => "system/modules/users");
  return $T;
}
function users_theme_table_header_row($Q,$default,$input,$params) {
	$html = "";
	$trclass = "";
	$tdclass = "";
		if (count($input)) {
			if (isset($params['tr-style'])) {
				$trclass = " class='".$params['tr-style']."' ";
			}
			$html .= "<tr{$trclass}>\n";
			foreach ($input as $cell) {
				if (isset($params['td-style'])) {
					$tdclass = " class='".$params['td-style']."'";
				}
				$html .= "<td{$tdclass} valign='top'>{$cell}</td>\n";
			}
			$html .= "</tr>\n";
		}
	return $html;
}
function users_theme_table_data_row($Q,$default,$input,$params) {
	$html = "";
	$trstyle = "";
	$tdstyle = "";
		if (count($input)) {
			if (isset($params['tr-style'])) {
				$trstyle = " class='".$params['tr-style']."'";
			}
			$html .= "<tr{$trstyle}>\n";
			foreach ($input as $cell) {
				if (isset($params['td-style'])) {
					$tdstyle = " class='".$params['td-style']."'";
				}
				$html .= "<td valign='top'{$tdstyle}>{$cell}</td>\n";
			}
			$html .= "</tr>\n";
		}
	return $html;
}
function users_synchronised_users($Q,$dblayer,$providers,$params) {
$page = array();
$page['body'] = "";
$sourceLabel = "";
$modref = "";
if (isset($params['sourceLabel'])) {
	$sourceLabel = $params['sourceLabel'];
}
if (isset($params['modref'])) {
	$modref = $params['modref'];
}
$task = "";
if (isset($_GET{'task'})) {
	$task = "&task=".GetParam('task','text','GET');
}
$sourceParam = "";
if (isset($_GET{'source'})) {
	$sourceParam = "&source=".GetParam('source','text','GET');
}
							//$usrdb = $default->param("USER","dbname");
							//$usrconn = $default->param("USER","dbconn");
							$current_user = current_user($dblayer);
							/* Pager Params */
							$maxRows = 10;
							$maxPageLinks = 10;
							$pageNum = 0;
							$startRow = 0;
							setupPagerParams($maxRows,$maxPageLinks,$pageNum,$startRow);
							$usersQuery = sprintf("select * from users where source = '%s'",$sourceLabel);
							$users = $dblayer->query("USER",sprintf("%s LIMIT %d,%d",$usersQuery,$startRow,$maxRows));
							$themedtable = array();
				
								if ($users->num_rows) {
									$tableheaders = array();
										$tableheaders[] = "Username";
										$tableheaders[] = "Real Name";
										$tableheaders[] = "Auth Source";
										$tableheaders[] = "Enabled";
										$tableheaders[] = "Actions";
									$tableparams = array();
										$tableparams['modref'] = $modref;
										$tableparams['profile'] = "table_header_row";
										$tableparams['tr-style'] = "table-header";
									$themedtable['header-row'] = theme($Q,null,$tableheaders,$tableparams);
							
										$tableparams['profile'] = "table_data_row";
										//$tableparams['td-style'] = "data-cell";//odd or even
									$themedtable['table-data-rows'] = "";	
										$editImage = null;
										if (isset($Q['icons']['map']['tiny']['actions']['edit'])) {
											$editImage = "<img valign='midlle' src='".$Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['tiny']['actions']['edit']."' />\n";
										}
										$deleteImage = null;
										if (isset($Q['icons']['map']['tiny']['actions']['delete'])) {
											$deleteImage = "<img valign='midlle' src='".$Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['tiny']['actions']['delete']."' />\n";
										}
										$oddeven = "a";
									while ($row = $users->fetch_object()) {
										$cellData = array();
										$cellData[] = $row->username;
										$cellData[] = $row->realname;
										$cellData[] = $row->source;
										$enabledImg = null;
										if ($row->enabled) {
											if (isset($Q['icons']['map']['tiny']['actions']['ok'])) {
												$enabledImg = "<img valign='midlle' src='".$Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['tiny']['actions']['ok']."' />\n";
											}
											$enableAction = "disable-user";
										} else {
											if (isset($Q['icons']['map']['tiny']['actions']['close'])) {
												$enabledImg = "<img valign='midlle' src='".$Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['tiny']['actions']['cancel']."' />\n";
											}
											$enableAction = "enable-user";
										}
						
										$cellData[] = "<a href=\"".$Q{'base_url'}."?module={$modref}{$task}{$sourceParam}&local-task={$enableAction}&UID=".$row->UID."&page={$pageNum}\">".$enabledImg."</a>\n";
										$cellData[] = $editImage."&nbsp;<a href=\"".$Q{'base_url'}."?module={$modref}{$task}{$sourceParam}&local-task=archive-user&UID=".$row->UID."&page={$pageNum}\">".$deleteImage."</a>\n";
										$tableparams['tr-style'] = "table-data-{$oddeven}";
										if ($oddeven == "a") {
											$oddeven = "b";
										} else {
											$oddeven = "a";
										}
										$themedtable['table-data-rows'] .= theme($Q,null,$cellData,$tableparams);
									} //while ($users->row = mysql_fetch_assoc($users->query));
									$page['body'] .= "<div class='data-table-wrap'><table class='data-table'>".$themedtable['header-row'].$themedtable['table-data-rows']."</table></div>\n";
								} else {
									$page['body'] .= "<p class=\"warning\">There are no users synchronised with this Authentication Source <em>{{$sourceLabel}}</em></p>\n";
								}
							//$page['body'] .= resultPagerHTML($usrdb,$usrconn,$usersQuery,$Q{'base_url'}."?module={$modref}{$task}{$sourceParam}",$maxRows,$maxPageLinks,$pageNum);
return $page['body'];
}
function users_forms($Q,$dblayer,$providers,$params) {
$form = null;
$modref = "users";
if (isset($params['modref'])) {
	$modref = $params['modref'];
}
	if (isset($params['form'])) {
		switch ($params['form']) {
		case "new-user":
			$form .= "<form action=\"".$Q{'base_url'}."/?module={$modref}&task=new-user\" method=\"POST\" name=\"newUserForm\">\n";
			$form .= "<table cellspacing=\"0\">\n";
				$usernameValue = "";
				if (isset($params{'fields'}{'username'})) {
					$usernameValue = "value=\"".$params{'fields'}{'username'}."\" ";
				}
				$displaynameValue = "";
				if (isset($params{'fields'}{'displayname'})) {
					$displaynameValue = "value=\"".$params{'fields'}{'displayname'}."\" ";
				}
				$initialGroupValue = 0;
				if (isset($params{'fields'}{'initialgroup'})) {
					$initialGroupValue = $params{'fields'}{'initialgroup'};
				}
				$form .= "<tr><td align=\"right\" valign=\"top\">Username:</td><td align=\"left\" valign=\"top\"><input type=\"text\" name=\"usernametext\" id=\"usernametext\" autocomplete=\"off\" {$usernameValue}/><br /><a href=\"#\">Check Availability</a></td></tr>\n";
				$form .= "<tr><td align=\"right\" valign=\"top\">Password:</td><td align=\"left\" valign=\"top\"><input type=\"password\" name=\"passwordtext\" id=\"passwordtext\" autocomplete=\"off\" /></td></tr>\n";
				$form .= "<tr><td align=\"right\" valign=\"top\">Repeat password:</td><td align=\"left\" valign=\"top\"><input type=\"password\" name=\"rpasswordtext\" id=\"rpasswordtext\" autocomplete=\"off\" /></td></tr>\n";
				$form .= "<tr><td align=\"right\" valign=\"top\">Display Name:</td><td align=\"left\" valign=\"top\"><input type=\"text\" name=\"displaynametext\" id=\"displaynametext\" {$displaynameValue}/></td></tr>\n";
				$form .= "<tr><td align=\"right\" valign=\"top\">Initial Group:</td>";
					$form .= "<td align=\"left\" valign=\"top\">";
					if ($groups = $dblayer->query("USER",sprintf("select * from groups order by label ASC"))) {
						if ($groups->num_rows) {
							$form .= "<select name=\"initialGroupSelect\" id=\"initialGroupSelect\">\n";
							$form .= "<option value=\"0\"";
								if ($initialGroupValue == 0) {
									$form .= " SELECTED ";
								}
							$form .= ">-- Choose Group --</option>\n";
							while ($row = $groups->fetch_object()) {
								$form .= "<option value=\"".$row->gid."\"";
									if ($initialGroupValue == $row->gid) {
										$form .= " SELECTED ";
									}
								$form .= ">".$row->label."</option>\n";
							}//while
							$form .= "</select>\n";
						} else {
							$form .= "<p>There are no groups to select</p>\n";
						}
					}
					$form .= "</td></tr>\n";
					$hidden = "";
					$hidden .= "<input type=\"hidden\" name=\"posted\" id=\"posted\" value=\"true\" />\n";
					//$hidden .= "<input type=\"\" name=\"\" id=\"\" value=\"\" />\n";
					//$hidden .= "<input type=\"\" name=\"\" id=\"\" value=\"\" />\n";
					//$hidden .= "<input type=\"\" name=\"\" id=\"\" value=\"\" />\n";
					$form .= "<tr><td align=\"right\" valign=\"top\" colspan=\"2\">{$hidden}<input type=\"submit\" name=\"submit\" value=\"Create User\" /></td></tr>\n";
				$form .= "</table>\n";
				$form .= "</form>\n";
		break;
		case "new-group":
			$groupValue = "";
			if (isset($params['fields']['groupLabelText'])) {
				$groupValue = "value=\"".$params['fields']['groupLabelText']."\" ";
			}
			$hidden = "<input type=\"hidden\" name=\"posted\" id=\"posted\" value=\"true\" />\n";
			$form .= "<form name=\"newGroupForm\" action=\"".$Q{'base_url'}."/?module={$modref}&task=new-group\" method=\"post\"><p>Group Label: <input type=\"text\" name=\"groupLabelText\" id=\"groupLabelText\" {$groupValue}/><br /><input type=\"submit\" name=\"submitGroupLabelButton\" id=\"submitGroupLabelButton\" value=\"Create\"/>{$hidden}</p></form>\n";
		break;
		}
	}
return $form;
}
function users_display_auth_source_config($source) {
$page = array();
$page['body'] = "";
	switch ($source['type']) {
	case "mysql":
							$page['body'] .= "<h2>Authentication Configuration</h2>\n";
							$page['body'] .= "<p>\n";
							$page['body'] .= "<strong>Type:</strong> ".$source['type']."<br />\n";
							$page['body'] .= "<strong>Database Host:</strong> ".$source['database']['dbhost']."<br />\n";
							$page['body'] .= "<strong>Database Name:</strong> ".$source['database']['dbname']."<br />\n";
							$page['body'] .= "<strong>Database User:</strong> ".$source['database']['dbuser']."<br />\n";
							$page['body'] .= "<strong>User Table:</strong> ".$source['fields']['table']."<br />\n";
							$page['body'] .= "</p>\n";
							$page['body'] .= "<h2>Syncronised Users</h2>\n";
	break;
	case "ldap":
	case "ldaps":
							$page['body'] .= "<h2>Authentication Configuration</h2>\n";
							$page['body'] .= "<p>\n";
							$page['body'] .= "<strong>Server:</strong> ".$source['server']['host'].":".$source['server']['port']."<br />\n";
							$page['body'] .= "<strong>Auth DN:</strong> ".$source['auth']['dn']."<br />\n";
							$page['body'] .= "<strong>Auth FQDN:</strong> ".$source['auth']['fqdn']."<br />\n";
							$page['body'] .= "<strong>Admin Bind User:</strong> ".$source['admin']['user']."<br />\n";
							$page['body'] .= "<strong>Admin DN:</strong> ".$source['admin']['dn']."<br />\n";
							$page['body'] .= "<strong>Admin FQDN:</strong> ".$source['admin']['fqdn']."<br />\n";
							$page['body'] .= "</p>\n";
							$page['body'] .= "<h2>Syncronised Users</h2>\n";
	break;
	}
return $page['body'];
}
function users_task($Q,$dblayer,$providers,$params) {
$page = array();
$page['title'] = "";
$page['body'] = "";
	if (isset($params['task'])) {
		$task = $params['task'];
		$modref = "";
		if (isset($params['modref'])) {
			$modref = $params['modref'];
		}
		$T = setup_custom_icons($Q);
  	$Q['icons']['users'] = load_icon_map($T);
		switch ($task) {
		default:
			//$T = setup_custom_icons($Q);
  		//$Q['icons']['users'] = load_icon_map($T);
  		/* */
  		$titleImageSrc = "";
  		$image = "";
  		if (isset($Q['icons']['users']['custom']['small']['users'])) {
  			$titleImageSrc = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['users']['custom']['small']['users'];
  		} else if (isset($Q['icons']['map']['small']['apps']['system-users'])) {
				$titleImageSrc = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['small']['apps']['system-users'];
  		}
  		if (($titleImageSrc) and ($titleImageSrc != "")) {
  			$image = "<img valign='middle' src='{$titleImageSrc}' />&nbsp;";
  		}
			$page['title'] = "User Management";
			$page['body'] .= "<h2>{$image}Local Users</h2>\n";
			/** Specifiy the default action **/
			$localTask = "";
			if (isset($_GET['local-task'])) {
				$localTask = GetParam("local-task","text","GET");
			}
			switch ($localTask) {
			case "archive-user":
							$UID = GetParam("UID","text","GET");
							$CUID = current_user($dblayer);
							if ($CUID['UID'] != $UID) {
								if ($UID) {
									$params = array();
									$params['UID'] = $UID;
									$page['body'] .= archive_user($Q,$dblayer,$providers,$params);
								}
							} else {
								$page['body'] .= "<p class=\"error\">You cannot archive the account you are currently logged in with.</p>\n";
							}
							$params = array();
							$params['sourceLabel'] = 'local';
							$params['modref'] = $modref;
							$page['body'] .= users_synchronised_users($Q,$dblayer,$providers,$params);
						break;
			case "disable-user":
				//$page['body'] .= "<p class=\"warning\">Disable User Button Clicked</p>\n";
				$UID = GetParam("UID","text","GET");
				$CUID = current_user($dblayer);
				if ($CUID['UID'] != $UID) {
					if ($UID) {
						$upd = $dblayer->query("USER",sprintf("update users set enabled=0 where UID = '%s'",$UID));
						$uinfo = user_info($dblayer,$UID);
						$page['body'] .= "<p class=\"ok\">User <em>(".$uinfo['username'].")</em> Disabled</p>\n";
					}
				} else {
					$page['body'] .= "<p class=\"error\">You cannot disable the account you are currently logged in with.</p>\n";
				}
				$params = array();
				$params['sourceLabel'] = 'local';
				$params['modref'] = $modref;
				$page['body'] .= users_synchronised_users($Q,$dblayer,$providers,$params);
			break;
			case "enable-user":
				//$page['body'] .= "<p class=\"warning\">Enable User Button Clicked</p>\n";
				$UID = GetParam("UID","text","GET");
				$CUID = current_user($dblayer);
				if ($CUID['UID'] != $UID) {
					if ($UID) {
						$upd = $dblayer->query("USER",sprintf("update users set enabled=1 where UID = '%s'",$UID));
						$uinfo = user_info($dblayer,$UID);
						$page['body'] .= "<p class=\"ok\">User <em>(".$uinfo['username'].")</em> Enabled</p>\n";
					}
				} else {
					$page['body'] .= "<p class=\"error\">You cannot enable the account you are currently logged in with.</p>\n";
				}
				$params = array();
				$params['sourceLabel'] = 'local';
				$params['modref'] = $modref;
				$page['body'] .= users_synchronised_users($Q,$dblayer,$providers,$params);
			break;
			default:
				$params = array();
				$params['sourceLabel'] = 'local';
				$params['modref'] = $modref;
				$page['body'] .= users_synchronised_users($Q,$dblayer,$providers,$params);
			}//switch
		break;
		case "new-user":
			$modImageSrc = "";
  		if (isset($Q['icons']['users']['custom']['small']['users'])) {
  			$modImageSrc = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['users']['custom']['small']['users'];
  		} else if (isset($Q['icons']['map']['small']['apps']['system-users'])) {
				$modImageSrc = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['small']['apps']['system-users'];
  		}
  		if (($modImageSrc) and ($modImageSrc != "")) {
  			$image = "<img valign='middle' src='{$modImageSrc}' />&nbsp;";
  		}
			$page['title'] .= "Create New User";
			$page['body'] .= "<h2>{$image}Create New User</h2>\n";
			if (isset($_POST['posted'])) {
				//form has been posted.
				$emsg = array();
				$fail = false;
				$minunlength = 4;
				$minpwlength = 8;
				$username = GetParam("usernametext","text","POST");
				$pass = GetParam("passwordtext","text","POST");
				$pass1 = GetParam("rpasswordtext","text","POST");
				$displayname = GetParam("displaynametext","text","POST");
				$initialGroup = GetParam("initialGroupSelect","int","POST");
				$source = "local";
				if ($initialGroup) {
					$unlength = strlen($username);
					if ($unlength >= $minunlength) {
						$plen = strlen($pass);
						$plen1 = strlen($pass1);
						if  ($pass == $pass1) {
							if ($plen >= $minpwlength && $plen1 >= $minpwlength) {
								if (strlen($displayname) >= 1) {
									//$dbname = $default->param("USER","dbname");
									//$dbconn = $default->param("USER","dbconn");
									//$lookup = new mysqldb;
									//$lookup->setquery($dbname,$dbconn,sprintf("select UID from users where username = '%s'",$username));
									$lookup = $dblayer->query("USER",sprintf("select UID from users where username = '%s'",$username));
									if (!$lookup->num_rows) {
										// we should be good to go!
										//$usr = new mysqldb;
										//$grp = new mysqldb;
										$UID = generate_user_UID($dblayer);
										$page['body'] .= "<p>UID:{$UID}</p>\n";
										$usrquery = sprintf("insert into users (UID,username,password,realname,enabled,source) values ('%s','%s','%s','%s',1,'%s')",$UID,$username,md5($pass),$displayname,$source);
										//$usr->setquery($dbname,$dbconn,$usrquery,1);
										$usr = $dblayer->query("USER",$usrquery);
										if (isset($usr['insert_id'])) {
											$userid = $usr['insert_id'];
										} else {
											$userid = 0;
										}
										//$userid = $usr->insert_id;
										if ($userid) {
											$grpquery = sprintf("insert into group_members (gid,id) values (%d,%d)",$initialGroup,$userid);
											//$grp->setquery($dbname,$dbconn,$grpquery,1);
											$grp = $dblayer->query("USER",$grpquery);
											$grpid = 0;
											if (isset($grp['insert_id'])) {
												$grpid = $grp['insert_id'];
											}
											//$grpid = $grp->lastId;
											if ($grpid) {
												//total success :)
											} else {
												$fail = true;
												$emsg[] = "The user ({$username}) has been created but there was an error adding the new user to the group!";
											}
										} else {
											$fail = true;
											$emsg[] = "There was an error adding the user to the database.";
										}
									} else {
										$fail = true;
										$emsg[] = "Username is taken!";
									}
									//$lookup->disposequery();
									
								} else {
									$fail = true;
									$emsg[] = "Your display name should be more than one character!";
								}
							} else {
								$fail = true;
								$emsg[] = "Your password must be longer than $minpwlength character[s]!";
							}
						} else {
							$fail = true;
							$emsg[] = "Your passwords do not match!";
						}
					} else {
						$fail = true;
						$emsg[] = "You must give a valid username longer than $minunlength character[s]!";
					}
				} else {
					$fail = true;
					$emsg[] = "You must select an Initial Group to add this user to!";
				}
				if ($fail) {
					//creating user failed, output $emsg
					foreach ($emsg as $msg) {
						$page['body'] .= "<p class=\"error\">{$msg}</p>\n";
					}
					$p = array();
					$p['form'] = "new-user";
					$p{'fields'}{'username'} = $username;
					$p{'fields'}{'displayname'} = $displayname;
					$p{'fields'}{'initialgroup'} = $initialGroup;
					$page['body'] .= users_forms($Q,$dblayer,$providers,$p);
				} else {
					//success!
					$page['body'] .= "<p class=\"ok\">User {$username} created successfully</p>\n";
					$p = array();
					$p['form'] = "new-user";
					$page['body'] .= users_forms($Q,$dblayer,$providers,$p);
				}
			} else {
				$p = array();
				$p['form'] = "new-user";
				$page['body'] .= users_forms($Q,$dblayer,$providers,$p);
			}//else show form
		break;
		case "new-group":
			$modImageSrc = "";
  		if (isset($Q['icons']['users']['custom']['small']['users'])) {
  			$modImageSrc = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['users']['custom']['small']['users'];
  		} else if (isset($Q['icons']['map']['small']['apps']['system-users'])) {
				$modImageSrc = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['small']['apps']['system-users'];
  		}
  		if (($modImageSrc) and ($modImageSrc != "")) {
  			$image = "<img valign='middle' src='{$modImageSrc}' />&nbsp;";
  		}
			$page['title'] .= "Create New User Group";
			$page['body'] .= "<h2>{$image}Create New User Group</h2>\n";
			if (isset($_POST['posted'])) {
				//we are posting the form
				$emsg = array();
				$fail = false;
				$mingrouplength = 3;
				$grouptext = GetParam("groupLabelText","text","POST");
				$gtl = strlen($grouptext);
				if ($gtl >= $mingrouplength) {
					//$dbname = $default->param("USER","dbname");
					//$dbconn = $default->param("USER","dbconn");
					//$lookup = new mysqldb;
					//$lookup->setquery($dbname,$dbconn,sprintf("select * from groups where label = '%s'",$grouptext));
					$lookup = $dblayer->query("USER",sprintf("select * from groups where label = '%s'",$grouptext));
						if ($lookup->num_rows) {
							$fail = true;
							$emsg[] = "The group label already exists!";
						} else {
							//add
							//$ins = new mysqldb;
							//$ins->setquery($dbname,$dbconn,sprintf("insert into groups (label) values ('%s')",$grouptext),1);
							$ins = $dblayer->query("USER",sprintf("insert into groups (label) values ('%s')",$grouptext));
							$lastID = 0;
							if (isset($ins['insert_id'])) {
								$lastID = $ins['insert_id'];
							}
							if ($lastID) {
								$page['body'] .= "<p class=\"ok\">Successfully added the group ({$grouptext})</p>\n";
								$p = array();
								$p['form'] = "new-group";
								$page['body'] .= users_forms($Q,$dblayer,$providers,$p);
							} else {
								$fail = true;
								$emsg[] = "An error occurred adding the group to the database!";
							}
						}
				} else {
					$fail = true;
					$emsg[] = "The group label must be longer than {$mingrouplength} character[s]";
				}
				if ($fail) {
					//creating user failed, output $emsg
					foreach ($emsg as $msg) {
						$page['body'] .= "<p class=\"error\">{$msg}</p>\n";
						$p = array();
						$p['form'] = "new-group";
						$p{'fields'}{'groupLabelText'} = $grouptext;
						$page['body'] .= users_forms($Q,$dblayer,$providers,$p);
					}
				}
			} else {
				//show create group form
				$p = array();
				$p['form'] = "new-group";
				$page['body'] .= users_forms($Q,$dblayer,$providers,$p);
				//$page['body'] .= "<p>Group Label: <input type=\"text\" name=\"groupLabelText\" id=\"groupLabelText\" /><br /><input type=\"button\" name=\"submitGroupLabelButton\" id=\"submitGroupLabelButton\" value=\"Create\"/></p>\n";
			}
		break;
		case "groups":
			//$T = setup_custom_icons($Q);
  		//$Q['icons']['users'] = load_icon_map($T);
  		/* */
  		$modImageSrc = "";
  		if (isset($Q['icons']['users']['custom']['small']['users'])) {
  			$modImageSrc = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['users']['custom']['small']['users'];
  		} else if (isset($Q['icons']['map']['small']['apps']['system-users'])) {
				$modImageSrc = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['small']['apps']['system-users'];
  		}
  		if (($modImageSrc) and ($modImageSrc != "")) {
  			$image = "<img valign='middle' src='{$modImageSrc}' />&nbsp;";
  		}
			$page['title'] .= "User Group Management";
			$page['body'] .= "<h2>{$image}Local Groups</h2>\n";
			//$db = $default->param("SYSTEM","dbname");
			//$dbconn = $default->param("SYSTEM","dbconn");
			//$groups = new mysqldb;
			//$groups->setquery($db,$dbconn,sprintf("select * from groups order by label ASC"));
			$groups = $dblayer->query("USER",sprintf("select * from groups order by label ASC"));
				if ($groups->num_rows) {
					$tableheaders = array();
  					$tableheaders[] = "Group";
  					$tableheaders[] = "Actions";
  				$tableparams = array();
  					$tableparams['modref'] = $modref;
  					$tableparams['profile'] = "table_header_row";
  					$tableparams['tr-style'] = "table-header";
  				$themedtable['header-row'] = theme($Q,null,$tableheaders,$tableparams);
  				
  				$tableparams['profile'] = "table_data_row";
  					//$tableparams['td-style'] = "data-cell";
  				$themedtable['table-data-rows'] = "";	
  				
  					$editImage = null;
						if (isset($Q['icons']['map']['tiny']['actions']['edit'])) {
							$editImage = "<img valign='midlle' src='".$Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['tiny']['actions']['edit']."' />\n";
						}
						$deleteImage = null;
						if (isset($Q['icons']['map']['tiny']['actions']['delete'])) {
							$deleteImage = "<img valign='midlle' src='".$Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['tiny']['actions']['delete']."' />\n";
						}
						$usersImage = null;
  					if (isset($Q['icons']['users']['custom']['tiny']['users'])) {
  						$usersImage = "<img valign='midlle' src='".$Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['users']['custom']['tiny']['users']."' />\n";
  					}
  					$oddeven = "a";
					while ($row = $groups->fetch_object()) {
						$cellData = array();
						$cellData[] = $row->label;
						$cellData[] = $editImage."&nbsp;".$deleteImage."&nbsp;".$usersImage;
						//$page['body'] .= "<p>".$groups->row{'label'}."</p>\n";
						$tableparams['tr-style'] = "table-data-{$oddeven}";
						if ($oddeven == "a") {
							$oddeven = "b";
						} else {
							$oddeven = "a";
						}
						$themedtable['table-data-rows'] .= theme($Q,null,$cellData,$tableparams);
					}// while ($groups->row = mysql_fetch_assoc($groups->query));
					$page['body'] .= "<div class='data-table-wrap'><table class='data-table'>".$themedtable['header-row'].$themedtable['table-data-rows']."</table></div>\n";
				} else {
					$page['body'] .= "<p>There are no Groups?</p>\n";
				}
		break;
		case "authsource":
			if (isset($_GET['source'])) {
				$sourceLabel = GetParam("source","text","GET");
				$source = $providers->provider_config($sourceLabel);
				if ($source) {
					$type = $providers->type($sourceLabel);
					switch ($type) {
					case "mysql":
						$authImageSrc = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['users']['custom']['medium']['authsource']['mysql'];
						$authImageSrcTiny = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['users']['custom']['small']['authsource']['mysql'];
						$authImage = "<img valign='middle' src='{$authImageSrc}' />\n";
						$page['title'] = "$sourceLabel Authentication Source";
						$page['body'] .= "<h2>{$authImage} {$sourceLabel} Authentication Source</h2>\n";
						/* Setup some tasks for managing the ldap servers */
						$icons = array();
							$icon = array();
								$icon['label'] = "MySQL Groups";
								$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=authsource&source={$sourceLabel}&local-task=mysql-groups";
								$icon['icon'] = $authImageSrcTiny;
								$icon['group'] = null;
							$icons[] = $icon;
							
							$params = array();
							$params['modref'] = $modref;
							$params['profile'] = "local-task";
						$page['body'] .= theme($Q,null,$icon,$params);
								$icon['label'] = "Search Directory";
								$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=authsource&source={$sourceLabel}&local-task=search-directory";
								$icon['icon'] = $authImageSrcTiny;
								$icon['group'] = null;
							$icons[] = $icon;
						$page['body'] .= theme($Q,null,$icon,$params);
						
						$page['body'] .= "<div class='clearboth'></div>\n";
						
						$localTask = "";
						if (isset($_GET['local-task'])) {
							$localTask = GetParam("local-task","text","GET");
						}
						switch ($localTask) {
						default:
							/*
							$page['body'] .= "<h2>Authentication Configuration</h2>\n";
							$page['body'] .= "<p>\n";
							$page['body'] .= "<strong>Type:</strong> ".$source['type']."<br />\n";
							$page['body'] .= "<strong>Database Host:</strong> ".$source['database']['dbhost']."<br />\n";
							$page['body'] .= "<strong>Database Name:</strong> ".$source['database']['dbname']."<br />\n";
							$page['body'] .= "<strong>Database User:</strong> ".$source['database']['dbuser']."<br />\n";
							$page['body'] .= "<strong>User Table:</strong> ".$source['fields']['table']."<br />\n";
							//$page['body'] .= "<strong>Admin FQDN:</strong> ".$source['admin']['fqdn']."<br />\n";
							$page['body'] .= "</p>\n";
							$page['body'] .= "<h2>Syncronised Users</h2>\n";*/
							
							/*$dbs = new dbloader;
							$dbs->add($source['label'],$source['database']);
							
							$dbname = $dbs->param('TEST-USERS','dbname');
							$dbconn = $dbs->param('TEST-USERS','dbconn');*/
							$page['body'] .= users_display_auth_source_config($source);
							$params = array();
							$params['sourceLabel'] = $sourceLabel;
							$params['modref'] = $modref;
							$page['body'] .= users_synchronised_users($Q,$dblayer,$providers,$params);
						break;
						case "archive-user":
							$page['body'] .= users_display_auth_source_config($source);
							$UID = GetParam("UID","text","GET");
							$CUID = current_user($default);
							if ($CUID['UID'] != $UID) {
								if ($UID) {
									$params = array();
									$params['UID'] = $UID;
									$page['body'] .= archive_user($Q,$dblayer,$providers,$params);
								}
							} else {
								$page['body'] .= "<p class=\"error\">You cannot archive the account you are currently logged in with.</p>\n";
							}
							$params = array();
							$params['sourceLabel'] = $sourceLabel;
							$params['modref'] = $modref;
							$page['body'] .= users_synchronised_users($Q,$dblayer,$providers,$params);
						break;
						case "disable-user":
							$page['body'] .= users_display_auth_source_config($source);
							$UID = GetParam("UID","text","GET");
							$CUID = current_user($dblayer);
							if ($CUID['UID'] != $UID) {
								if ($UID) {
									$upd = $dblayer->query("USER",sprintf("update users set enabled=0 where UID = '%s'",$UID));
									$uinfo = user_info($dblayer,$UID);
									$page['body'] .= "<p class=\"ok\">User <em>(".$uinfo['username'].")</em> Disabled</p>\n";
								}
							} else {
								$page['body'] .= "<p class=\"error\">You cannot disable the account you are currently logged in with.</p>\n";
							}
							$params = array();
							$params['sourceLabel'] = $sourceLabel;
							$params['modref'] = $modref;
							$page['body'] .= users_synchronised_users($Q,$dblayer,$providers,$params);
						break;
						case "enable-user":
							$page['body'] .= users_display_auth_source_config($source);
							$UID = GetParam("UID","text","GET");
							$CUID = current_user($dblayer);
							if ($CUID['UID'] != $UID) {
								if ($UID) {
									$upd = $dblayer->query("USER",sprintf("update users set enabled=1 where UID = '%s'",$UID));
									$uinfo = user_info($dblayer,$UID);
									$page['body'] .= "<p class=\"ok\">User <em>(".$uinfo['username'].")</em> Enabled</p>\n";
								}
							} else {
								$page['body'] .= "<p class=\"error\">You cannot enable the account you are currently logged in with.</p>\n";
							}
							$params = array();
							$params['sourceLabel'] = $sourceLabel;
							$params['modref'] = $modref;
							$page['body'] .= users_synchronised_users($Q,$dblayer,$providers,$params);
						break;
						}
					break;
					case "ldap":
					case "ldaps"://only ldaps can modify entries on an Active Directory server.
						$authImageSrc = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['users']['custom']['medium']['authsource']['ldap'];
						$authImageSrcTiny = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['users']['custom']['small']['authsource']['ldap'];
						$authImage = "<img valign='middle' src='{$authImageSrc}' />\n";
						$page['title'] = "$sourceLabel Authentication Source";
						$page['body'] .= "<h2>{$authImage} {$sourceLabel} Authentication Source</h2>\n";
						/* Setup some tasks for managing the ldap servers */
						$icons = array();
							$icon = array();
								$icon['label'] = "Ldap Groups";
								$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=authsource&source={$sourceLabel}&local-task=ldap-groups";
								$icon['icon'] = $authImageSrcTiny;
								$icon['group'] = null;
							$icons[] = $icon;
							
							$params = array();
							$params['modref'] = $modref;
							$params['profile'] = "local-task";
						$page['body'] .= theme($Q,null,$icon,$params);
								$icon['label'] = "Search Directory";
								$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=authsource&source={$sourceLabel}&local-task=search-directory";
								$icon['icon'] = $authImageSrcTiny;
								$icon['group'] = null;
							$icons[] = $icon;
						$page['body'] .= theme($Q,null,$icon,$params);
						
						$page['body'] .= "<div class='clearboth'></div>\n";
						
						$localTask = "";
						if (isset($_GET['local-task'])) {
							$localTask = GetParam("local-task","text","GET");
						}
						switch ($localTask) {
						default:
							/*$page['body'] .= "<h2>Authentication Configuration</h2>\n";
							$page['body'] .= "<p>\n";
							$page['body'] .= "<strong>Server:</strong> ".$source['server']['host'].":".$source['server']['port']."<br />\n";
							$page['body'] .= "<strong>Auth DN:</strong> ".$source['auth']['dn']."<br />\n";
							$page['body'] .= "<strong>Auth FQDN:</strong> ".$source['auth']['fqdn']."<br />\n";
							$page['body'] .= "<strong>Admin Bind User:</strong> ".$source['admin']['user']."<br />\n";
							$page['body'] .= "<strong>Admin DN:</strong> ".$source['admin']['dn']."<br />\n";
							$page['body'] .= "<strong>Admin FQDN:</strong> ".$source['admin']['fqdn']."<br />\n";
							$page['body'] .= "</p>\n";
							$page['body'] .= "<h2>Syncronised Users</h2>\n";*/
							$page['body'] .= users_display_auth_source_config($source);
							$params = array();
							$params['sourceLabel'] = $sourceLabel;
							$params['modref'] = $modref;
							$page['body'] .= users_synchronised_users($Q,$dblayer,$providers,$params);
						break;
						case "archive-user":
							$page['body'] .= users_display_auth_source_config($source);
							$UID = GetParam("UID","text","GET");
							$CUID = current_user($dblayer);
							if ($CUID['UID'] != $UID) {
								if ($UID) {
									$params = array();
									$params['UID'] = $UID;
									$page['body'] .= archive_user($Q,$dblayer,$providers,$params);
								}
							} else {
								$page['body'] .= "<p class=\"error\">You cannot archive the account you are currently logged in with.</p>\n";
							}
							$params = array();
							$params['sourceLabel'] = $sourceLabel;
							$params['modref'] = $modref;
							$page['body'] .= users_synchronised_users($Q,$dblayer,$providers,$params);
						break;
						case "disable-user":
							$page['body'] .= users_display_auth_source_config($source);
							$UID = GetParam("UID","text","GET");
							$CUID = current_user($dblayer);
							if ($CUID['UID'] != $UID) {
								if ($UID) {
									$upd = $dblayer->query("USER",sprintf("update users set enabled=0 where UID = '%s'",$UID));
									$uinfo = user_info($dblayer,$UID);
									$page['body'] .= "<p class=\"ok\">User <em>(".$uinfo['username'].")</em> Disabled</p>\n";
								}
							} else {
								$page['body'] .= "<p class=\"error\">You cannot disable the account you are currently logged in with.</p>\n";
							}
							$params = array();
							$params['sourceLabel'] = $sourceLabel;
							$params['modref'] = $modref;
							$page['body'] .= users_synchronised_users($Q,$dblayer,$providers,$params);
						break;
						case "enable-user":
							$page['body'] .= users_display_auth_source_config($source);
							$UID = GetParam("UID","text","GET");
							$CUID = current_user($dblayer);
							if ($CUID['UID'] != $UID) {
								if ($UID) {
									$upd = $dblayer->query("USER",sprintf("update users set enabled=1 where UID = '%s'",$UID));
									$uinfo = user_info($dblayer,$UID);
									$page['body'] .= "<p class=\"ok\">User <em>(".$uinfo['username'].")</em> Enabled</p>\n";
								}
							} else {
								$page['body'] .= "<p class=\"error\">You cannot enable the account you are currently logged in with.</p>\n";
							}
							$params = array();
							$params['sourceLabel'] = $sourceLabel;
							$params['modref'] = $modref;
							$page['body'] .= users_synchronised_users($Q,$dblayer,$providers,$params);
						break;
						case "config":
							//authsource Info
							$page['body'] .= users_display_auth_source_config($source);
						break;
						case "ldap-groups":
							/* Display any ldap group mappings for this authentication source */
							$page['body'] .= 
								"<p>LDAP Group Mappings allow users to authenticate against an LDAP server and automatically be mapped to".
								" an internal safe-harbour group mapping</p>\n";
							$page['body'] .= 
								"<p>To add group mappings please add to the current configuration file for this ldap authentication source.</p>\n";
							if (isset($source['maps'])) {
								$tableheaders = array();
									$tableheaders[] = "Group DN";
									$tableheaders[] = "Local Group";
								$tableparams = array();
									$tableparams['modref'] = $modref;
									$tableparams['profile'] = "table_header_row";
									$tableparams['tr-style'] = "table-header";
								$themedtable['header-row'] = theme($Q,null,$tableheaders,$tableparams);
									$tableparams['profile'] = "table_data_row";
									//$tableparams['td-style'] = "data-cell";//odd or even
									//$tableparams['tr-style'] = "data-row";
								$themedtable['table-data-rows'] = "";
								$oddeven = "a";
								foreach ($source['maps'] as $map) {
									$cellData = array();
									$cellData[] = $map['dn'];
									$cellData[] = group_label($dblayer,$map['group']);
									$tableparams['tr-style'] = "table-data-{$oddeven}";
										if ($oddeven == "a") {
											$oddeven = "b";
										} else {
											$oddeven = "a";
										}
									//$page['body'] .= $map['dn']." => ".group_label($default,$map['group']);
									$themedtable['table-data-rows'] .= theme($Q,null,$cellData,$tableparams);
								}
								$page['body'] .= "<div class='data-table-wrap'><table class='data-table'>".$themedtable['header-row'].$themedtable['table-data-rows']."</table></div>\n";
							}
						break;
						case "sync-user":
							$info = array();
							$info['username'] = GetParam("username","text","POST");
							$info['realname'] = GetParam("realname","text","POST");
							$UID = synchronise_user($dblayer,$providers,$info,$sourceLabel);
							if ($UID) {
								//synchronised
								$page['body'] .= "<p class=\"ok\">User <em>(".$info['username'].")</em> synchronised successfully.</p>\n";
							} else {
								//not synchronised
								$page['body'] .= "<p class=\"error\">User <em>(".$info['username'].")</em> not synchronised!</p>\n";
							}
						break;
						case "search-directory":
								$page['body'] .= "<h2>LDAP Directory Search</h2>\n";
									$formAction = $Q{'base_url'}."/?module={$modref}&task=authsource&source={$sourceLabel}&local-task=search-directory";
								$page['body'] .= "<form name='{$sourceLabel}-directory-search-form' action='{$formAction}' method='post'>\n";
									$page['body'] .= "<p><input type='text' name='sterm' /><input type='submit' name='submit' value='Search {$sourceLabel} Directory' /></p>\n";
								$page['body'] .= "</form>\n";
							if (isset($_POST['sterm'])) {
								//we are searching and displaying results
								$authSource = $source;
								$sterm = GetParam("sterm","text","POST");
								$page['body'] .= "<h2>Search Results</h2>\n";
								//$page['body'] .= "<p>You searched for \"{$sterm}\"</p>\n";
								//perform an LDAP search
								if ( (isset($authSource['server']['host'])) and (isset($authSource['server']['port'])) ) {
									$ldap_conn = ldap_connect($authSource['server']['host'],$authSource['server']['port']);
								}
								if ($ldap_conn) {
									switch ($authSource['ldap_provider']) {
										case "ms-active-directory":
											//if ($authSource['type'] == "ldaps") {
												ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION,3);
												ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS,0);
											//}
											
										break;
									}//switch
									//echo $authSource['admin']['dn']." ".$authSource['auth']['username_label']." ".$authSource['auth']['username_label']."={$sterm}*";
									ldap_bind($ldap_conn,$authSource['admin']['user']."@".$authSource['admin']['fqdn'],$authSource['admin']['pass']) or die("Cannot Bind to Directory server!");
									$searchFilter = "(|(".$authSource['auth']['username_label']."={$sterm}*)(cn={$sterm}*)(cn=*{$sterm})(cn=*{$sterm}*))";
									$lresult = ldap_search($ldap_conn,$authSource['admin']['dn'],$searchFilter);
									$lcount = ldap_count_entries($ldap_conn,$lresult);
									if ($lcount) {
										//check that user is not already registered with modbase, no duplicates
			
										#$fe = ldap_first_entry($MBCONFIG{'LDAP_CONN'},$lresult);
			
										#$fe = ldap_next_entry($MBCONFIG{'LDAP_CONN'},$lresult);
										$fe = ldap_get_entries($ldap_conn,$lresult);
										#$udn = ldap_get_dn($MBCONFIG{'LDAP_CONN'},$fe);
										$i = 0;
										$displayed = 0;
										$computers = 0;
										$others = 0;
										$tableheaders = array();
											$tableheaders[] = "Real Name";
											$tableheaders[] = "Username";
											$tableheaders[] = "Actions";
										$tableparams = array();
											$tableparams['modref'] = $modref;
											$tableparams['profile'] = "table_header_row";
											$tableparams['tr-style'] = "table-header";
										$themedtable['header-row'] = theme($Q,null,$tableheaders,$tableparams);
										$themedtable['table-data-rows'] = "";
										$oddeven = "a";
										do {
										
											$cellData = array();
											if (isset($fe[$i]['samaccountname'][0])) {
												if (!strpos($fe[$i]['samaccountname'][0],'$')) {
													$cellData[] = str_ireplace($sterm,"<strong>$sterm</strong>",$fe[$i]['cn'][0]);
													$cellData[] = str_ireplace($sterm,"<strong>$sterm</strong>",$fe[$i]['samaccountname'][0]);
													$cellData[] = "<form name=\"syncUserForm\" action=\"".$Q{'base_url'}."/?module={$modref}&task={$task}&local-task=sync-user&source={$sourceLabel}\" method=\"POST\"><input type=\"hidden\" name=\"username\" value=\"".$fe[$i]['samaccountname'][0]."\"><input type=\"hidden\" name=\"realname\" value=\"".$fe[$i]['cn'][0]."\"><input type=\"submit\" name=\"syncUserButton\" value=\"Synchronise User\"></form>\n";
													$tableparams['tr-style'] = "table-data-{$oddeven}";
														if ($oddeven == "a") {
															$oddeven = "b";
														} else {
															$oddeven = "a";
														}
													//$page['body'] .= $map['dn']." => ".group_label($default,$map['group']);
													$themedtable['table-data-rows'] .= theme($Q,null,$cellData,$tableparams);
													$displayed++;
												} else {
													$computers++;
												}
											} else {
												$others++;
											}
										$i++;
										} while ($i < $lcount);
										if ($lcount <> 1) {
											$lrecords = "records";
										} else {
											$lrecords = "record";
										}
										if ($displayed <> 1) {
											$drecords = "records";
										} else {
											$drecords = "record";
										}
										if ($computers <> 1) {
											$fcomputers = "computer accounts";
										} else {
											$fcomputers = "computer account";
										}
										if ($others <> 1) {
											$fothers = "other accounts";
										} else {
											$fothers = "other account";
										}
										$page['body'] .= "<div class='data-table-wrap'><table class='data-table'>".$themedtable['header-row'].$themedtable['table-data-rows']."</table></div>\n";
										$page['body'] .= "<p>Showing {$displayed} {$drecords}, $lcount {$lrecords} returned. {$computers} {$fcomputers} filered. {$others} {$fothers}</p>\n";
										//$page['body'] .= "<p><em>Please Note: Accounts containing a '$' character have been filtered, these are usually computer accounts.</em></p>\n";
									} else {
										//display_info_message("User '{$sterm}' not found!");
									} 
									ldap_unbind($ldap_conn);
									ldap_free_result($lresult);
								}
								
							} else {
								//we are displaying the default ldap search form
								/*$page['body'] .= "<h2>LDAP Directory Search</h2>\n";
									$formAction = $Q{'base_url'}."/?module={$modref}&task=authsource&source={$sourceLabel}&local-task=search-directory";
								$page['body'] .= "<form name='{$sourceLabel}-directory-search-form' action='{$formAction}' method='post'>\n";
									$page['body'] .= "<p><input type='text' name='sterm' /><input type='submit' name='submit' value='Search {$sourceLabel} Directory' /></p>\n";
								$page['body'] .= "</form>\n";*/
							}
						break;
						}//switch local-task
					break;
					}//switch type
				}
			}
		break;
		}//switch task
	}
return $page;
}
function users_title($Q,$dblayer,$params) {

			$T = setup_custom_icons($Q);
  		$P = load_icon_map($T);
  		/* */
  		$modImageSrc = "";
  		if (isset($P['custom']['large']['users'])) {
  			$modImageSrc = $Q['base_url']."/".$T['icons']['location']."/".$P['custom']['large']['users'];
  		} else if (isset($Q['icons']['map']['large']['apps']['system-users'])) {
				$modImageSrc = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['large']['apps']['system-users'];
  		}
	$title = "<h1><img valign='middle' src='{$modImageSrc}' />&nbsp;User Management</h1>\n";
	
	return $title;
}
function users_breadcrumb($Q,$dblayer,$params) {
			if (isset($params['modref'])) {
				$modref = $params['modref'];
			} else {
				$modref = "users";
			}
				
				//$module->setquery($db,$conn,sprintf("select title from modules where modref='%s' and enabled = 1",$modref));
				$module = $dblayer->query("SYSTEM",sprintf("select title from modules where modref='%s' and enabled = 1",$modref));
					if ($module->num_rows) {
						$row = $module->fetch_object();
						$entry['label'] = "cPanel";
						$entry['link'] = $Q{'base_url'};
						$entries[] = $entry;
							$url = $Q{'base_url'}."/?module=".$modref;
						$entry['label'] = $row->title;
						$entry['link'] = $url;
						$entries[] = $entry;
						if (isset($_GET{'task'})) {
							$task = GetParam("task","text","GET");
							$url .= "&task=".$task;
							switch ($task) {
							default:
								$entry['label'] = $task;
								$entry['link'] = $Q{'base_url'}."/?module=".$modref;
								$entries[] = $entry;	
							break;
							case "authsource":
								//do not display an entry for this.
							break;
							}
							if (isset($_GET{'source'})) {
								$source = GetParam("source","text","GET");
								$url .= "&source={$source}";
								$entry['label'] = $source;
								$entry['link'] = $url;
								$entries[] = $entry;
								if (isset($_GET{'local-task'})) {
									$localtask = GetParam("local-task","text","GET");
									switch ($localtask) {
									default:
										$url .= "&local-task={$localtask}";
										$entry['label'] = $localtask;
										$entry['link'] = $url;
										$entries[] = $entry;
									break;
									case "search-directory":
										$url .= "&local-task={$localtask}";
										$entry['label'] = "Search Directory";
										$entry['link'] = $url;
										$entries[] = $entry;
									break;
									case "ldap-groups":
										$url .= "&local-task={$localtask}";
										$entry['label'] = "Directory Group Mappings";
										$entry['link'] = $url;
										$entries[] = $entry;
									break;
									case "mysql-groups":
										$url .= "&local-task={$localtask}";
										$entry['label'] = "MySQL Group Mappings";
										$entry['link'] = $url;
										$entries[] = $entry;
									break;
									}//switch
								}
							}
						}
			} else {
				$entry['label'] = "cPanel";
				$entry['link'] = null;
				$entries[] = $entry;
			}
	return $entries;
}
function users_cpanel($Q,$dblayer,$params) {
	/* 
		This module is called when the cPanel is asked to display this module's icon.
		If this functon does not exist, then a simply entry is used for this modules cPanel icon.
		
		This function should return an icon array
			$icons = array();
			$icon = array();
				$icon['label'] = "";
				$icon['link'] = "";
				$icon['icon'] = "";
				$icon['group'] = "";
			$icons[] = $icon;
				$icon['label'] = "";
				$icon['link'] = "";
				$icon['icon'] = "";
				$icon['group'] = "";
			$icons[] = $icon;
			//repeat for as many icons as you wish to display. 
	*/
			$T = setup_custom_icons($Q);
  		$Q['icons']['users'] = load_icon_map($T);
  		/* */
  		$modImageSrc = "";
  		if (isset($Q['icons']['users']['custom']['large']['users'])) {
  			$modImageSrc = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['users']['custom']['large']['users'];
  		} else if (isset($Q['icons']['map']['large']['apps']['system-users'])) {
				$modImageSrc = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['large']['apps']['system-users'];
  		}
	$modref = "users";
	if (isset($params['modref'])) {
		$modref = $params['modref'];
	}
	$icons = array();
		$icon = array();
			$icon['label'] = "Users";
			$icon['link'] = $Q{'base_url'}."/?module={$modref}";
			$icon['icon'] = $modImageSrc;
			$icon['group'] = "Administration";
		$icons[] = $icon;
	return $icons;
}
function users_dependant_modules() {
	/* returns an array of modref of the modules to load when loading this module */
	$modules = array();
		/*$modules[] = "MyModref";
		$modules[] = "MyOtherModref";*/
	return $modules;
}
function users_local_tasks($Q,$dblayer,$providers,$params) {
	/*
		called before users_task() as part of the module loading process.
			Returns an array of icons
	*/
	$T = setup_custom_icons($Q);
	$Q['icons']['users'] = load_icon_map($T);
	/* */
	$modImageSrc = "";
	if (isset($Q['icons']['users']['custom']['small']['users'])) {
		$usersIcon = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['users']['custom']['small']['users'];
	} else if (isset($Q['icons']['map']['large']['apps']['system-users'])) {
		$usersIcon = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['small']['apps']['system-users'];
	}
	
	$modref = "users";
	if (isset($params['modref'])) {
		$modref = $params['modref'];
	}
	$icons = array();
		$icon = array();
			$icon['label'] = "Local Users";
			$icon['link'] = $Q{'base_url'}."/?module={$modref}";
			$icon['icon'] = $usersIcon;
			$icon['group'] = "Users";//probably will not be used
		$icons[] = $icon;
			$icon['label'] = "Local Groups";
			$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=groups";
			$icon['icon'] = $usersIcon;
			$icon['group'] = "Users";
		$icons[] = $icon;
		
			/* Blank Divider (clearboth class)*/
			$addGroupIcon = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['small']['actions']['contact-new'];
			$addUserIcon = $addGroupIcon;
			$icon['label'] = "Local Tasks";
			$icon['link'] = null;
			$icon['icon'] = null;
			$icon['group'] = null;
		$icons[] = $icon; 
			$icon['label'] = "New User";
			$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=new-user";
			$icon['icon'] = $addUserIcon;
			$icon['group'] = "Users";
		$icons[] = $icon;
			$icon['label'] = "New Group";
			$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=new-group";
			$icon['icon'] = $addGroupIcon;
			$icon['group'] = "Users";
		$icons[] = $icon;
	/* Authentication Sources */	
	$labels = $providers->labels();
	if (count($labels) ) {
		$icon['label'] = "Authentication Sources";
			$icon['link'] = null;
			$icon['icon'] = null;
			$icon['group'] = null;
		$icons[] = $icon;
		foreach ($labels as $label) {
			$type = $providers->type($label);
			switch ($type) {
			case "mysql":
				$authIcon = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['users']['custom']['small']['authsource']['mysql'];
				$icon['label'] = $label;
				$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=authsource&source={$label}";
				$icon['icon'] = $authIcon;
				$icon['group'] = "Users";
			$icons[] = $icon;
			break;
			case "ldap":
			case "ldaps":
				$authIcon = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['users']['custom']['small']['authsource']['ldap'];
				$icon['label'] = $label;
				$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=authsource&source={$label}";
				$icon['icon'] = $authIcon;
				$icon['group'] = "Users";
			$icons[] = $icon;
			break;
			default:
				$authIcon = $Q['base_url']."/".$T['icons']['location']."/".$Q['icons']['users']['custom']['small']['authsource']['default'];
				$icon['label'] = $label;
				$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=authsource&source={$label}";
				$icon['icon'] = $authIcon;
				$icon['group'] = "Users";
			$icons[] = $icon;
			break;
			}
			//$page['body'] .= "<li>{$label} <em>{$type}</em></li>\n";
		}
	}
	return $icons;
}
?>
